import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
const myPlugin = (store) => {
  // 当 store 初始化后调用
  store.subscribe((mutation, state) => {
    // 每次 mutation 之后调用
    // mutation 的格式为 { type, payload }
    localStorage.setItem('state', JSON.stringify(state))
  })
}

export default new Vuex.Store({
  state: {
    token: JSON.parse(localStorage.getItem('state'))?.token || '',
  },
  plugins: [myPlugin],
  getters: {},
  mutations: {
    set_token(state, token) {
      state.token = token
    },
  },
  actions: {},
  modules: {},
})
